package com.alibaba.pet.service.impl;

import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.pet.domain.Pet;
import com.alibaba.pet.domain.PetDetail;
import com.alibaba.pet.mapper.PetDetailMapper;
import com.alibaba.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
        @Autowired
        private PetDetailMapper petDetailMapper;


        @Override
        @Transactional
        public void add(Pet pet) {
            //添加宠物 - 自增id
            super.add(pet);

            //添加宠物详情
            PetDetail petDetail = pet.getPetDetail();
            if(petDetail!=null){
                petDetail.setPetId(pet.getId());
                petDetailMapper.add(petDetail);
            }
        }


        @Override
        @Transactional
        public void remove(Long id) {
            //删除宠物信息
            super.remove(id);
            //删除宠物详情
            petDetailMapper.removeByPetId(id);
        }

        @Override
        @Transactional
        public void update(Pet pet) {
            //修改宠物信息
            super.update(pet);

            //修改宠物详情
            PetDetail petDetail = pet.getPetDetail();
            if(petDetail!=null){
                petDetail.setPetId(pet.getId());
                petDetailMapper.update(petDetail);
            }
        }

        @Override
        @Transactional
        public void patchRemove(List<Long> ids) {
            //删除宠物信息
            super.patchRemove(ids);
            //删除宠物详情
            petDetailMapper.patchRemoveByPetId(ids);
        }
}
